package com.pdg.android.guessmynumber;

import java.util.Iterator;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class GuessMyNumberActivity extends Activity {
    private static final String INITIAL_GUESSES_SO_FAR = "";
    private static final String INITIAL_GUESS = "";
	private static final String GUESSES_SO_FAR_SEPARATOR = ", ";
	private static final String INVALID_GUESS_VALUE = "???";
	private static final String OPEN_PAREN = "(";
	private static final String CLOSE_PAREN = ")";

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GameData.startGame();
        refreshDisplay();
    }
    
    private void refreshDisplay() {
    	updatePrompt();
    	updateGuessCount();
    	updateGuessesSoFar();
	}
    
    private void updateGuessesSoFar() {
    	TextView tv = (TextView)findViewById(R.id.guessesSoFar);
    	String theGuesses = null;
    	Iterator<GuessRecord> iterator = GameData.getHistoryIterator();
    	while(iterator.hasNext()){
    		GuessRecord theRecord = iterator.next();
    		if(theGuesses==null){
    			theGuesses = INITIAL_GUESSES_SO_FAR;
    		}else{
    			theGuesses+=GUESSES_SO_FAR_SEPARATOR;
    		}
    		if(theRecord.getGuess()==null){
    			theGuesses += INVALID_GUESS_VALUE;
    		}else{
    			theGuesses += theRecord.getGuess().toString();
    		}
    		theGuesses += OPEN_PAREN+getStatusForGuessState(theRecord.getState())+CLOSE_PAREN;
    	}
    	if(theGuesses==null){
    		theGuesses = INITIAL_GUESSES_SO_FAR;
    	}
    	tv.setText(String.format(getString(R.string.guesses_so_far_format),theGuesses));
	}

	private String getStatusForGuessState(GuessState state) {
		switch(state){
		case TOO_HIGH:
			return getString(R.string.guess_too_high_status);
		case TOO_LOW:
			return getString(R.string.guess_too_low_status);
		case CORRECT:
			return getString(R.string.guess_correct_status);
		case OUT_OF_RANGE:
			return getString(R.string.guess_out_of_range_status);
		default:
			return getString(R.string.guess_invalid_status);
		}
	}

	private void updateGuessCount() {
    	TextView tv = (TextView)findViewById(R.id.guessCount);
    	tv.setText(String.format(getString(R.string.guess_count_format),GameData.getGuessCount()));
	}

	public void guessButtonPressed(View theView){
    	TextView tv = (TextView)findViewById(R.id.currentGuess);
    	try{
    		Integer theGuess = Integer.parseInt(tv.getText().toString());
    		GameData.addGuess(theGuess);
    	}catch(Exception e){
    		e.printStackTrace();
    		GameData.addGuess(null);
    	}
    	tv.setText(INITIAL_GUESS);
		refreshDisplay();
		if(GameData.isGameOver()){
			GameData.startGame();
		}
    }
    
    private void updatePrompt(){
    	TextView tv = (TextView)findViewById(R.id.prompt);
    	if(GameData.getLastGuess()==null){
    		tv.setText(getString(R.string.guess_my_number));
    	}else{
    		switch(GameData.getLastGuess().getState()){
    		case TOO_LOW:{
    			tv.setText(getString(R.string.guess_too_low));
    		}break;
    		case TOO_HIGH:{
    			tv.setText(getString(R.string.guess_too_high));
    		}break;
    		case CORRECT:{
    			tv.setText(getString(R.string.guess_correct));
    		}break;
    		default:{
    			tv.setText(getString(R.string.invalid_guess));
    		}break;
    		}
    	}
    }
}